package com.chujian.Activities;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chujian.Adapter.FoodListAdapter;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Threads.HttpClientGetThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.model.Food;
import com.chujian.orderingsystem.R;

public class FoodListActivity extends Activity implements OnClickListener,
	OnItemLongClickListener,OnItemClickListener{
	private ListView mListView;
	private Button mBtnAddFood;
	private ImageButton mIbtnBack;
	private List<Food> mList;
	private Handler mHandler;
	private String categary;
	private TextView mTvTitle;
	private FoodListAdapter mAdapter;
	private int deletePosition;
	private int modifyPosition;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.food_list);
		initView();
		mHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				super.handleMessage(msg);
				String str = (String) msg.obj;
				if (str.equals("deleteSuccess")) {
					Toast.makeText(FoodListActivity.this, "删除成功",Toast.LENGTH_SHORT).show();
					mList.remove(deletePosition);
					mAdapter.notifyDataSetChanged();
				} else if (str.equals("deleteFailed")) {
					Toast.makeText(FoodListActivity.this, "删除失败",
							Toast.LENGTH_SHORT).show();
				} else {
					mList = JsonUtil.ListFoodFromJson(str);
					mAdapter=new FoodListAdapter(mList,
							FoodListActivity.this);
					mListView.setAdapter(mAdapter);
				}
			};
		};
		Intent intent=getIntent();
		categary=intent.getStringExtra("foodCategary");
		mTvTitle.setText(categary);
//		联网获取数据
		String url=Constan.BASE_URL+"FoodListServlet"+"?categary="+categary;
		new HttpClientGetThread(url,mHandler).start();;
	}

	private void initView() {
		// TODO Auto-generated method stub
		mListView=(ListView) findViewById(R.id.lv_food_list);
		mBtnAddFood=(Button) findViewById(R.id.btn_foodlist_add);
		mIbtnBack=(ImageButton) findViewById(R.id.ib_header_back);
		mTvTitle=(TextView) findViewById(R.id.tv_foodlist_title);
		mIbtnBack.setOnClickListener(this);
		mBtnAddFood.setOnClickListener(this);
		mListView.setOnItemClickListener(this);
		mListView.setOnItemLongClickListener(this);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode==0)return ;
		Food food=(Food) data.getSerializableExtra("food");
		if(1000==requestCode){
			if(1001==resultCode){
				mList.add(food);
				mAdapter.notifyDataSetChanged();
			}else if(1002==resultCode){
				mList.get(modifyPosition).setFoodName(food.getFoodName());
				mList.get(modifyPosition).setFoodId(food.getFoodId());
				mList.get(modifyPosition).setFoodPrice(food.getFoodPrice());
				mList.get(modifyPosition).setFoodUnit(food.getFoodUnit());
				mList.get(modifyPosition).setFoodUrl(food.getFoodUrl());
				mList.get(modifyPosition).setFoodStatus(food.getFoodStatus());
			
				mAdapter.notifyDataSetChanged();
			}
		}
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_foodlist_add:
			Intent intent =new Intent(FoodListActivity.this, AddFoodActivity.class);
			intent.putExtra("foodCategary", categary);
			startActivityForResult(intent, 1000);
			break;
		case R.id.ib_header_back:
			FoodListActivity.this.finish();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		Toast.makeText(FoodListActivity.this, "item long", Toast.LENGTH_SHORT).show();
		// TODO Auto-generated method stub
		deleteOrModifyDialog(position);
		return true;
	}
	private void deleteOrModifyDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(FoodListActivity.this);
        builder.setTitle(mList.get(position).getFoodName());
        builder.setMessage("请选择操作");
        builder.setPositiveButton("删除", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	confirmDialog(position);
            }
        });
        builder.setNegativeButton("修改", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
	            modifyPosition=position;
	            Food food=mList.get(position);
	            Intent intent=new Intent();  
	            intent.setClass(FoodListActivity.this, AddFoodActivity.class);  
	            Bundle bundle=new Bundle();  
	            bundle.putSerializable("food", food);//序列化  
	            intent.putExtras(bundle);//发送数据  
	            startActivityForResult(intent, 1000);//启动intent  

            }
        });
        builder.show();
	}
	private void confirmDialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(FoodListActivity.this);
        builder.setTitle(mList.get(position).getFoodName());
        builder.setMessage("确认删除吗？");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	deletePosition=position;
            	String url=Constan.BASE_URL+"DeleteFood";
            	new HttpClientPostThread(url, mHandler,mList.get(position).getFoodName()).start();;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
            	
            }
        });
        builder.show();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Toast.makeText(FoodListActivity.this, "item", Toast.LENGTH_SHORT).show();
//		deleteOrModifyDialog(position);
	}
}
